<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="allocateInfoList">
      <el-table-column label="日期" align="center" prop="dateInfo" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInfo, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="个人信息" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.trealname }} -- {{ scope.row.sex === 1 ? '男' : '女' }} -- {{ scope.row.age }}岁
        </template>
      </el-table-column>
      <el-table-column label="所属地" align="center">
        <template slot-scope="scope">
          {{ scope.row.province }} -- {{ scope.row.city }}
        </template>
      </el-table-column> 
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile }} 
        </template>
      </el-table-column>
    
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="dazhaohu(scope.row)">打招呼</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="haiVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <p style="text-align: center;">确认要把该用户放回公海？</p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :title="'打招呼'" :visible.sync="dialogVisible1" width="30%">
      <el-form :model="greetForm">
        <el-form-item>
          <el-input v-model="greetForm.msgContent" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="confirmGreeting">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}

.dialog-footer {
  text-align: right;
}
</style>
<script>
import { baseInfoData, listAllocateInfo, updateAllocateInfo, toSayHello } from "@/api/merchantdata/allocateInfo";
import { updatePoolInfo } from "@/api/merchantdata/poolInfo";
import Member from '@/views/merchantdata/poolInfo/member.vue';
export default {
  name: "AllocateInfo",
  components: {
    Member
  },
  data() {
    return {
      detailsDialogVisible: false, // 弹窗可见状态
      selectedRow: null,
      dialogVisible: false,
      dialogVisible1: false,
      haiVisible: false,
      currentOperation: null,
      dialogTitle: "",
      updateType: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      currentUid: false,
      // 显示搜索条件
      showSearch: true,

      // 总条数
      total: 0,
      // 公海数据分配表格数据
      allocateInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      greetForm: {
        msgContent: '' // 打招呼内容
      },
      currentRow: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        infoId: null,
        deptId: null,
        userId: null,
        dataId: null,
        uid: null,
        status: null,
        type: null,
      },
      // 表单参数
      form: {
        uid: null,
        remarkInfo: null,
      },
      // 表单校验
      rules: {
        remarkInfo: [
          { required: true, message: "请输入客户跟踪", trigger: "blur" }
        ],
        starNumber: [
          { required: true, message: "请输入客户星级", trigger: "blur" }
        ]
      },
      extData: {},
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {

    getTypeText(type) {
      switch (type) {
        case 1:
          return '管理员分配';
        case 2:
          return '自己捞取';
        case 3:
          return '系统推送';
      }
    },

    getOptions() {
      baseInfoData().then((response) => {
        this.extData = response.data;
      });
    },
    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.uid)
    },

    /** 查询公海数据分配列表 */
    getList() {
      this.loading = true;
      listAllocateInfo(this.queryParams).then(response => {
        this.allocateInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.dialogVisible = false;
      this.haiVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        infoId: null,
        deptId: null,
        userId: null,
        dataId: null,
        uid: null,
        status: null,
        type: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleUpdate(row, operation) {
      this.reset();
      this.form.dataId = row.dataId;
      this.currentOperation = operation; // 设置当前操作类型
      this.dialogVisible = true;
    },

    handleUpdates(row) {
      this.reset();
      this.form.dataId = row.dataId;
      this.status = 0;
      this.userId = 0;
      this.haiVisible = true;
    },
    dazhaohu(row) {
      this.currentRow = row;
      this.dialogVisible1 = true; // 打开弹窗
    },

    confirmGreeting() {
      const query = {
        loginUid: this.currentRow.uid,
        msgContent: this.greetForm.msgContent
      };
      toSayHello(query)
        .then(response => {
          this.$message.success('发送成功');
          this.dialogVisible1 = false;
          this.greetForm.msgContent = '';
        })
        .catch(error => {
          this.$message.error('发送失败');
        });
    },


    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let updateData = this.form;
          updateData = {
            dataId: this.form.dataId,
            stutas: 0,
            userId: this.form.userId
          };
          updateAllocateInfo(updateData).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.haiVisible = false;
            this.getList();
          });
        }
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let updateData = this.form;
          if (this.currentOperation === 'remake') {
            updateData = {
              id: this.form.dataId,
              remarkInfo: this.form.remarkInfo
            };
          }
          else if (this.currentOperation === 'star') {
            updateData = {
              id: this.form.dataId,
              starNumber: this.form.starNumber
            };
          }
          else if (this.currentOperation === 'info') {
            updateData = {
              id: this.form.dataId,
              stateInfo: this.form.stateInfo
            };
          }
          updatePoolInfo(updateData).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
